#include "tag_addr_setting.h"

#include <QScreen>
#include <QGuiApplication>

bool TagAddrSetWidget::tag_server_connected = false;

TagAddrSetWidget::TagAddrSetWidget(QWidget* parent):QWidget(parent)
{
    int idx = 0;
    QScreen* screen = QGuiApplication::primaryScreen();
    QSize size = screen->size();
    this->setFixedSize(size);

    m_layout = new QGridLayout();
    m_layout->setAlignment(Qt::AlignCenter);
    m_layout->setContentsMargins(10, 10, 10, 50);

    m_tag_server = new QLabel();
    m_tag_server->setText("tag server");
    m_tag_server->setStyleSheet("background-color: rgb(8, 111, 160);");
    m_tag_server->setAlignment(Qt::AlignCenter);
    m_layout->addWidget(m_tag_server, idx, 0, 1, 2);

    m_tag_server_ip_label = new QLabel();
    m_tag_server_ip_label->setText("tag server ip: ");
    m_layout->addWidget(m_tag_server_ip_label, ++idx, 0, 1, 1);

    m_tag_server_ip_line = new QLineEdit();
    m_tag_server_ip_line->setText("192.168.2.4");
    m_layout->addWidget(m_tag_server_ip_line, idx, 1, 1, 1);

    m_tag_server_ip_port = new QLabel();
    m_tag_server_ip_port->setText("tag server port: ");
    m_layout->addWidget(m_tag_server_ip_port, ++idx, 0, 1, 1);

    m_tag_server_port = new QLineEdit();
    m_tag_server_port->setText("8899");
    m_layout->addWidget(m_tag_server_port, idx, 1, 1, 1);

    m_connect = new QPushButton();
    m_connect->setText("Disconnecting. Press to connect.");
    m_connect->setStyleSheet("background-color: rgb(128, 0, 0);");
    m_layout->addWidget(m_connect, ++idx, 0, 1, 2);

    m_layout->setRowStretch(0, 0);
    m_layout->setRowStretch(1, 0);
    m_layout->setRowStretch(2, 0);
    m_layout->setRowStretch(3, 0);

    m_layout->setColumnStretch(0, 0);
    m_layout->setColumnStretch(1, 0);

    m_split_label = new QLabel();
    m_split_label->setText("");
    m_layout->addWidget(m_split_label, ++idx, 0, 1, 2);

    m_all_tag_init = new QPushButton();
    m_all_tag_init->setText("Press to start resetting addr...");
    m_connect->setStyleSheet("background-color: rgb(128, 0, 0);");
    m_layout->addWidget(m_all_tag_init, ++idx, 0, 1, 2);

    m_recv_addr_label = new QLabel();
    m_recv_addr_label->setText("received Mac addr: ");
    m_layout->addWidget(m_recv_addr_label, ++idx, 0, 1, 1);

    m_recv_addr_line = new QLineEdit();
    m_recv_addr_line->setReadOnly(true);
    m_layout->addWidget(m_recv_addr_line, idx, 1, 1, 1);

    m_set_addr_label = new QLabel();
    m_set_addr_label->setText("input the Mac addr here: ");
    m_layout->addWidget(m_set_addr_label, ++idx, 0, 1, 1);

    m_setted_addr = new QLineEdit();
    m_layout->addWidget(m_setted_addr, idx, 1, 1, 1);

    m_cmd_send = new QPushButton();
    m_cmd_send->setText("Press to Set...");
    m_cmd_send->setStyleSheet("background-color: rgb(0, 255, 0);");
    m_layout->addWidget(m_cmd_send, ++idx, 0, 1, 2);

    m_layout->setRowStretch(4, 0);
    m_layout->setRowStretch(5, 0);
    m_layout->setRowStretch(6, 0);
    m_layout->setRowStretch(7, 0);
    m_layout->setRowStretch(8, 0);

    qDebug() << "TagAddrSetWidget size: " << this->size();

    this->setLayout(m_layout);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    m_layout->setAlignment(Qt::AlignCenter);

    connect(m_connect, SIGNAL(released()), this, SLOT(connect_tag_server_check()));
}

void TagAddrSetWidget::connect_tag_server_check()
{
    if(!tag_server_connected) {
        m_connect->setText("Connecting. Press to disconnect.");
        m_connect->setStyleSheet("background-color: rgb(0, 255, 0);");
    } else {
        m_connect->setText("Disconnecting. Press to connect.");
        m_connect->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    tag_server_connected = !tag_server_connected;
}

TagAddrSetWidget::~TagAddrSetWidget() {}
